package com.student.system.service.impl;

import com.student.system.dao.StudentDao;
import com.student.system.entity.StudentScore;
import com.student.system.entity.Studentmessage;
import com.student.system.entity.Studentregister;
import com.student.system.service.StudentService;
import com.student.system.utils.MD5Utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//业务层
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao=new StudentDao();

    public boolean checkLoginId(String userName) throws SQLException {
        return studentDao.checkLoginId(userName);
    }

    /**
     * 用户注册
     * @param req
     * @param resp
     */
    @Override
    public void doRegister(HttpServletRequest req, HttpServletResponse resp) {
        Studentregister studentregister =new Studentregister();
        studentregister.setUserName(req.getParameter("userName"));
        studentregister.setPhone(req.getParameter("phone"));
        studentregister.setPassword(MD5Utils.md5plus(req.getParameter("password")));
        try {
            studentDao.save(studentregister);
            resp.getWriter().print("ok");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            resp.getWriter().print("error");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doMessage(HttpServletRequest req, HttpServletResponse resp) {
        Studentmessage studentmessage = new Studentmessage();
        studentmessage.setName(req.getParameter("name"));
        studentmessage.setStudentId(req.getParameter("studentId"));
        studentmessage.setPhone(req.getParameter("phone"));
        studentmessage.setIdentity(req.getParameter("identity"));

        studentmessage.setSex(req.getParameter("sex"));
        try{
            studentmessage.setAge(Integer.valueOf(req.getParameter("age")));
        }catch (Exception e){}

        studentmessage.setEmail(req.getParameter("email"));
        studentmessage.setMajor(req.getParameter("major"));
        studentmessage.setIdentityId(req.getParameter("identityId"));

        try {
            studentDao.savem(studentmessage);
            resp.getWriter().print("ok");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            resp.getWriter().print("error");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void doLogin(HttpServletRequest req, HttpServletResponse resp) {
        String userName = req.getParameter("userName");
        try {
            Studentregister studentregister = studentDao.findByusername(userName);
            if(studentregister==null){
                resp.getWriter().print("用户不存在，请重新注册");
                return;
            }
            String password = req.getParameter("password");
            if(!MD5Utils.md5plus(password).equals(studentregister.getPassword())){
                resp.getWriter().print("账号密码错误，请重新输入");
                return;
            }
            resp.getWriter().print("ok");
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void checkLoginId(HttpServletRequest req, HttpServletResponse resp) {
        String account= req.getParameter("userName");
        try {
            boolean exist = studentDao.checkLoginId(account);
            if (exist){
                resp.getWriter().print("exist");  //用户名已经存在
            }else{
                resp.getWriter().print("ok"); //用户名不存在
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                resp.getWriter().print(throwables.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void StudentScoreMessage(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<StudentScore> list = studentDao.studentScoreList();
            req.setAttribute("list",list);
            req.getRequestDispatcher("/jsp/student/StudentScore.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }


}
